package com.jhhc.Weathers.Picture.Controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jhhc.Weathers.Picture.Pojo.*;
import com.jhhc.utils.ImageBase64;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;

import com.jhhc.Weathers.Picture.Service.BDMS_UD_PICTURE_BService;
import com.jhhc.Weathers.Picture.Service.BDMS_UD_PICTURE_CService;
import com.jhhc.utils.JsonResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author LYQ
 */
@Api(value="PictureController",tags={"气象数据服务"})
@RestController
public class PictureController {

	@Autowired
	private BDMS_UD_PICTURE_CService bdms_UD_PICTURE_CService;

	@Autowired
	private BDMS_UD_PICTURE_BService bdms_UD_PICTURE_BService;
	@Value("${datasourceType}")
	String datasourceType;
	@ApiOperation(value = "获取产品信息", notes = "获取产品信息",httpMethod = "GET")
	@RequestMapping(value = "Weathers/Picture/GetProductInfos", method = RequestMethod.GET)
	public JsonResult getProductInfos(@ApiParam(name = "productType", value = "产品类型", required = true) @RequestParam("productType") String productType) {
		JsonResult jsonResult = new JsonResult();
		List<BDMS_UD_PICTURE_C> BDMS_UD_PICTURE_CS = bdms_UD_PICTURE_CService.selectByClassID(productType,datasourceType);
		List<ProductInfo> ProductInfos = new ArrayList<ProductInfo>();//返回对象集合
		List<String> list = new ArrayList<String>();
		ProductInfo productInfo = null;
		int length = productType.length();
		for (int i = 0; i < BDMS_UD_PICTURE_CS.size(); i++) {
			String[] division = BDMS_UD_PICTURE_CS.get(i).getClassid().split(productType);//用参数productType进行字符串分割
			String[] source = division[0].split(">");//用“>”进行二次分割
			if (source[0].equals("")) {
				if (!list.contains(productType)) {//判断list中是否存在，不存在放入list
					list.add(productType);
				}
			}else {
				if (!list.contains(source[0])) {//判断list中是否存在，不存在放入list
					list.add(source[0]);
				}
			}
		}
		for (int i = 0; i < list.size(); i++) {
			Pattern pattern = Pattern.compile(list.get(i));
			productInfo = new ProductInfo();
			productInfo.setProductSource(list.get(i));
			for (int j = 0; j < BDMS_UD_PICTURE_CS.size(); j++) {
				Matcher matcher = pattern.matcher(BDMS_UD_PICTURE_CS.get(j).getClassid());
				if (matcher.find()) {
					int subscript = BDMS_UD_PICTURE_CS.get(j).getClassid().indexOf(productType,0);
					int front = subscript + length;
					char n = BDMS_UD_PICTURE_CS.get(j).getClassid().charAt(front);
					String s3 = String.valueOf(n);
					if (s3.equals(">")){
						int position = subscript + length + 1;
						String character = BDMS_UD_PICTURE_CS.get(j).getClassid().substring(position);
//					String[] division = BDMS_UD_PICTURE_CS.get(j).getClassid().split(productType);
//					productInfo.setPictureTypes(division[1].substring(1));
						productInfo.setPictureTypes(character);
					}else {
						break;
					}
				}
			}
			ProductInfos.add(productInfo);
		}
		jsonResult.setStatusCode(200);
		jsonResult.setSucceeded(true);
		jsonResult.setData(ProductInfos);
		return jsonResult;
	}

	@ApiOperation(value = "获取图片信息列表")
	@RequestMapping(value = "Weathers/Picture/GetPictureList", method = RequestMethod.GET)
	public JsonResult getPictureList(@ApiParam(name = "productSource", value = "产品来源", required = true)@RequestParam String productSource,//产品来源
									 @ApiParam(name = "productType", value = "产品类型", required = true)@RequestParam String productType,//产品类型
									 @ApiParam(name = "pictureType", value = "图片类型", required = true)@RequestParam String pictureType,//图片类型
									 @ApiParam(name = "startTime", value = "开始时间", required = true)@RequestParam String startTime,//开始时间
									 @ApiParam(name = "endTime", value = "结束时间", required = true)@RequestParam String endTime) {//结束时间
		JsonResult jsonResult = new JsonResult();
		jsonResult.setStatusCode(200);
		jsonResult.setSucceeded(true);
		Datas datas = new Datas();
		List<PictureInfo> pictureList = new ArrayList<PictureInfo>();
		BDMS_UD_PICTURE_C bdms_ud_picture_c = bdms_UD_PICTURE_CService.selectByClassIDs(productSource,productType,pictureType,datasourceType);
		List<BDMS_UD_PICTURE_B> BDMS_UD_PICTURE_B = bdms_UD_PICTURE_BService.getPictureList(bdms_ud_picture_c.getClassid(), startTime, endTime);
		if (BDMS_UD_PICTURE_B.size() > 0) {
			PictureInfo getPicture = null;
			for (int i = 0; i < BDMS_UD_PICTURE_B.size(); i++) {
				getPicture = new PictureInfo();
				if (BDMS_UD_PICTURE_B.get(i).getDatatm() != null) {
					getPicture.setDatatm(BDMS_UD_PICTURE_B.get(i).getDatatm());
				}
				if (BDMS_UD_PICTURE_B.get(i).getAbsolutepath() != null) {
					getPicture.setAbsolutepath(BDMS_UD_PICTURE_B.get(i).getAbsolutepath());
				}
				if (BDMS_UD_PICTURE_B.get(i).getRelativepath() != null) {
					getPicture.setRelativepath(BDMS_UD_PICTURE_B.get(i).getRelativepath());
				}
				pictureList.add(getPicture);
			}
			datas.setPictureList(pictureList);
			jsonResult.setData(datas);
		}else {
			String maxDataTime = bdms_UD_PICTURE_BService.maxDataTime(bdms_ud_picture_c.getClassid());
			datas.setTime(maxDataTime);
			jsonResult.setData(datas);
		}
		return jsonResult;
	}

	@ApiOperation(value = "获取对应路径下的图片")
	@RequestMapping(value = "Weathers/Picture/GetSingle", method = RequestMethod.GET)
	public JsonResult getSingle(@ApiParam(name = "absolutepath", value = "绝对路径", required = true)@RequestParam String absolutepath,
								@ApiParam(name = "relativepath", value = "相对路径", required = true)@RequestParam String relativepath) throws Exception {
		JsonResult jsonResult = new JsonResult();

		String cmower = String.join(File.separator, absolutepath,relativepath);//用"/"拼接字符串
		ImageBase64 imageBase64 = new ImageBase64();
		String s = imageBase64.convertFileToBase64(cmower);//根据拼接的路径cmower，找到对应位置下的图片，图片转为Base64字符串
		String url = imageBase64.getURLEncoderString(s);
		jsonResult.setStatusCode(200);
		jsonResult.setSucceeded(true);
		jsonResult.setData(url);
		return jsonResult;
	}
	@ApiOperation(value = "获取对应路径下的多张图片")
	@RequestMapping(value = "Weathers/Picture/GetMultiple", method = RequestMethod.GET )
	public JsonResult getMultiple(@ApiParam(name = "absolutepath", value = "绝对路径", required = true)@RequestParam String absolutepath,
								  @ApiParam(name = "relativepath", value = "相对路径", required = true)@RequestParam String relativepath) throws Exception {
		JsonResult jsonResult = new JsonResult();
		List<PictureData> pictureDatas = new ArrayList<PictureData>();
		PictureData pictureData = null;
		String[] routes = relativepath.split("[+]");
		String[] route = routes[1].split(";");
		ImageBase64 imageBase64 = new ImageBase64();
		for (int i = 0; i < route.length; i++) {
			String[] picture = route[i].split(",");
			pictureData = new PictureData();
			String cmower = String.join("",routes[0] , picture[1]);
			String str = String.join(File.separator, absolutepath, cmower);
			String s = imageBase64.convertFileToBase64(str);
			String compress = imageBase64.getURLEncoderString(s);
			pictureData.setPictureTM(picture[0]);
			pictureData.setPictureStr(compress);
			pictureDatas.add(pictureData);
		}
		jsonResult.setStatusCode(200);
		jsonResult.setSucceeded(true);
		jsonResult.setData(pictureDatas);
		return jsonResult;
	}

//	@ApiOperation(value = "获取对应路径下的多张图片（缩略图）")
//	@RequestMapping(value = "Weathers/Picture/GetMultipleThumbnail", method = RequestMethod.GET )
//	public JsonResult getMultipleThumbnail(@RequestParam String absolutepath,
//										   @RequestParam String relativepath) throws Exception {
//		JsonResult jsonResult = new JsonResult();
//		List<PictureData> pictureDatas = new ArrayList<PictureData>();
//		PictureData pictureData = null;
//		String[] routes = relativepath.split("[+]");
//		String[] route = routes[1].split(";");
//		ImageBase64 imageBase64 = new ImageBase64();
//		for (int i = 0; i < route.length; i++) {
//			String[] picture = route[i].split(",");
//			pictureData = new PictureData();
//			String cmower = String.join("",routes[0], picture[1]);
//			String str = String.join(File.separator, absolutepath, cmower);
//			//BufferedImage sourceImg = ImageIO.read(new FileInputStream(new File(str)));
//			Thumbnails.of(str)
//					.size(360,360)
//					.toFile("D:\\江河汇成\\WebApi服务\\气象缩略图" + File.separator + picture[1]);
//			String s = imageBase64.convertFileToBase64("D:\\江河汇成\\WebApi服务\\气象缩略图" + File.separator + picture[1]);
//			String compress = imageBase64.getURLEncoderString(s);
//			pictureData.setPictureTM(picture[0]);
//			pictureData.setPictureStr(compress);
//			pictureDatas.add(pictureData);
//		}
//		jsonResult.setStatusCode(200);
//		jsonResult.setSucceeded(true);
//		jsonResult.setData(pictureDatas);
//		return jsonResult;
//	}
//	@ApiOperation(value = "获取对应路径下的图片（缩略图）")
//	@RequestMapping(value = "Weathers/Picture/GetSingleThumbnail", method = RequestMethod.GET)
//	public JsonResult getSingleThumbnail(@ApiParam(name = "absolutepath", value = "绝对路径", required = true)@RequestParam String absolutepath,
//										 @ApiParam(name = "relativepath", value = "相对路径", required = true)@RequestParam String relativepath) throws Exception {
//		JsonResult jsonResult = new JsonResult();
//		ImageBase64 imageBase64 = new ImageBase64();
//		String[] file =  relativepath.split("[+]");
//		String relativepaths =String.join("",file[0],file[1]);
//		String cmower = String.join(File.separator, absolutepath, relativepaths);//用"/"拼接字符串
//		Thumbnails.of(cmower)
//				.size(360,360)
//				.toFile("D:\\江河汇成\\WebApi服务\\气象缩略图" + File.separator + file[1]);
//		String s = imageBase64.convertFileToBase64("D:\\江河汇成\\WebApi服务\\气象缩略图"
//				+ File.separator + file[1]);
//		String url = imageBase64.getURLEncoderString(s);
//		jsonResult.setStatusCode(200);
//		jsonResult.setSucceeded(true);
//		jsonResult.setData(url);
//		return jsonResult;
//	}
}
